package com.local.coupon.controller;


import com.local.common.util.ResultUtil;
import com.local.coupon.pojo.UserCouponRecord;
import com.local.coupon.pojo.UserCouponRecordListRequestParam;
import com.local.coupon.service.UserCouponRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/usercouponrecord")
public class UserCouponRecordController {

    @Autowired
    private UserCouponRecordService userCouponRecordService;

    /**
     * 得到用户的可以正常使用的优惠券，用于订单支付
     * @param requestParam
     * @return
     */
    @GetMapping("/getListForPay")
    @ResponseBody
    public ResultUtil getListForPay(@RequestBody UserCouponRecordListRequestParam requestParam) {
        Integer userId = requestParam.getUserId();
        Integer priceTotal = requestParam.getPriceTotal();
        List<Integer> goodsIdList = requestParam.getGoodsIdList();
        List<UserCouponRecord> list = userCouponRecordService.getByUserIdForPay(userId, priceTotal, goodsIdList);
        if (Objects.nonNull(list)) {
            return new ResultUtil(200, "", list);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 通过id获取数据
     * @param userCouponRecordId
     * @return
     */
    @GetMapping("/{userCouponRecordId}")
    @ResponseBody
    public ResultUtil getById(@PathVariable("userCouponRecordId") Integer userCouponRecordId) {
        UserCouponRecord userCouponRecord = userCouponRecordService.getById(userCouponRecordId);
        if (Objects.nonNull(userCouponRecord)) {
            return new ResultUtil(200, "", userCouponRecord);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }
}
